package com.shujia.base;

import java.io.*;
import java.util.HashMap;

/*
    将所有map任务的结果文件进行合并，产生最终的结果文件，真实的mr中，reduce个数默认只有一个。
 */
public class ReduceTaskDemo {
    public static void main(String[] args) throws Exception{
        //获取所有的block块
        File file = new File("hadoop/data/maps");
        File[] blocks = file.listFiles();

        HashMap<String, Integer> map = new HashMap<>();

        BufferedReader br = null;
        if(blocks!=null){
            for (File block : blocks) {
                br = new BufferedReader(new FileReader(block));
                String line = null;
                while ((line=br.readLine())!=null){
                    //理科五班 9
                    String[] strings = line.split(" ");
                    String clazz = strings[0];
                    Integer number = Integer.valueOf(strings[1]);
                    if(map.containsKey(clazz)){
                        map.put(clazz,map.get(clazz)+number);
                    }else {
                        map.put(clazz,number);
                    }
                }
            }
        }

        BufferedWriter bw = new BufferedWriter(new FileWriter("hadoop/data/reduce/part-r-00000"));
        map.forEach((k,v)->{
            try {
                bw.write(k+" "+v);
                bw.newLine();
                bw.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

        assert br!=null;


        //释放资源
        bw.close();
        br.close();

    }
}